#pragma once

#include <iostream>
#include <functional>
#include <vector>
#include <ctime>
#include <unistd.h>


using namespace std;

typedef std::function<void()> task_t;

void Download()
{
    cout << "我是一个下载任务 -> " << "处理者: " << getpid() << endl;
}

void PrintLog()
{
    cout << "我是一个打印日志任务 -> " << "处理者: " << getpid() << endl;
}

void PushVideoStream()
{
    cout << "我是一个推送视频流任务 -> " << "处理者: " << getpid() << endl;
}


class Init
{
public:
    // 任务码
    const static int g_download_code = 0;
    const static int g_printlog_code = 1;
    const static int g_pushvideostream_code = 2;

    // 任务集合
    vector<task_t> tasks;
public:
    Init()
    {
        tasks.push_back(Download);
        tasks.push_back(PrintLog);
        tasks.push_back(PushVideoStream);

        srand(time(nullptr));
    }

    bool CheckSafe(int code)
    {
        if(code >=0 && code < tasks.size())
        {
            return true;
        }
        else
            return false;
    }

    void RunTask(int code)
    {
        return tasks[code]();
    }

    int SelectTask()
    {
        return rand() % tasks.size();
    }

    std::string ToDesc(int code)
    {
        switch(code)
        {
        case g_download_code:
            return "Download";
        case g_printlog_code:
            return "PrintLog";
        case g_pushvideostream_code:
            return "PushVideoStream";
        default:
            return "Unknow";
        }
    }

};

Init init; // 定义全局对象，一加载就会创建出来